<?php

class Bootstrapper {
    public static $__enviromentInitialized = false;
    public static $__configurationInitialized = false;
    public static $__doctrineInitialized = false;

    public static function initEnviroment() {
        defined('APPLICATION_PATH')
            or define('APPLICATION_PATH', dirname(__FILE__));

        defined('APPLICATION_ENVIRONMENT')
            or define('APPLICATION_ENVIRONMENT', 'development');

        error_reporting(E_ALL | E_STRICT);
        ini_set('display_startup_errors', 1);
        ini_set('display_errors', 1);
        date_default_timezone_set('Europe/Amsterdam');

        set_include_path(
            APPLICATION_PATH
            . PATH_SEPARATOR . APPLICATION_PATH . '/../lib'
            . PATH_SEPARATOR . APPLICATION_PATH . '/../lib/Doctrine'
            . PATH_SEPARATOR . APPLICATION_PATH . '/models'
            . PATH_SEPARATOR . APPLICATION_PATH . '/models/generated'
            . PATH_SEPARATOR . get_include_path()
        );

        require_once "Zend/Loader.php";
        Zend_Loader::registerAutoload();

        self::$__enviromentInitialized = true;
    }

    public static function initConfiguration() {
        if(self::$__enviromentInitialized == false) {
            Bootstrapper::initEnviroment();
        }

        $configuration = new Zend_Config_Ini(
            APPLICATION_PATH . '/config/app.ini',
            APPLICATION_ENVIRONMENT
        );

        $registry = Zend_Registry::getInstance();
        $registry->configuration = $configuration;

        self::$__configurationInitialized = true;
    }

    public static function initDoctrine() {
        if(self::$__configurationInitialized == false) {
            Bootstrapper::initConfiguration();
        }

        $registry = Zend_Registry::getInstance();

        $adapter = $registry->configuration->database->params->adap;
        $host = $registry->configuration->database->params->host;
        $username = $registry->configuration->database->params->username;
        $psssword = $registry->configuration->database->params->password;
        $dbname = $registry->configuration->database->params->dbname;
        
        Doctrine_Manager::connection($adapter.'://'.$username.':'.$psssword.'@'.$host.'/'.$dbname);

        Zend_Registry::set('doctrine_config', array(
            'data_fixtures_path'  =>  APPLICATION_PATH.'/doctrine/data/fixtures',
            'models_path'         =>  APPLICATION_PATH.'/models',
            'migrations_path'     =>  APPLICATION_PATH.'/doctrine/migrations',
            'sql_path'            =>  APPLICATION_PATH.'/doctrine/data/sql',
            'yaml_schema_path'    =>  APPLICATION_PATH.'/doctrine/schema'
        ));

        self::$__doctrineInitialized = true;
    }
    
    public static function bootstrap() {
        Bootstrapper::initEnviroment();
        Bootstrapper::initConfiguration();
        Bootstrapper::initDoctrine();
        
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
        $frontController->addModuleDirectory(APPLICATION_PATH . '/modules');
        $frontController->setParam('env', APPLICATION_ENVIRONMENT);

        Zend_Layout::startMvc(APPLICATION_PATH . '/layouts/scripts');
        $view = Zend_Layout::getMvcInstance()->getView();
        $view->doctype('XHTML1_STRICT');
    }
}
